<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <div>
      <div style="margin: 0 0 10px;float: left;">
        <router-link :to="{name: EditName}">
          <el-button type="primary" plain>添加</el-button>
        </router-link>
        <el-button type="danger" plain @click="delObjs()">删除</el-button>
      </div>

      <!--<div style=" float: right;">-->
      <!--<el-input placeholder="请输入间隔单位" v-model="inputObjName" class="input-with-select">-->
      <!--<el-button slot="append" icon="el-icon-search" @click="inputFunc()"></el-button>-->
      <!--</el-input>-->
      <!--</div>-->

      <div style=" float: right;">
        <el-input placeholder="请输入" v-model="inputObjName" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="inputFunc()"></el-button>
        </el-input>
      </div>

      <div>
        <!--内容组件-->
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange">

          <el-table-column
            type="selection"
            width="55">
          </el-table-column>

          <el-table-column
            prop="id"
            label="ID"
            width="200">
          </el-table-column>
          <el-table-column
            prop="every"
            label="间隔长度"
            width="500">
          </el-table-column>

          <el-table-column
            prop="period"
            label="间隔单位"
            width="400">
            <template slot-scope="scope">
              <el-select disabled v-model="scope.row.period" placeholder="请选择间隔单位" style="width: 100%;">
                <el-option
                  v-for="(item,index) in optionsPeriod"
                  :key="index"
                  :label="item.value+' / '+item.id"
                  :value="item.id"
                ></el-option>
              </el-select>
            </template>

          </el-table-column>

          <el-table-column
            prop="count"
            label="关联任务数"
            width="100">
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            width="200">
            <template slot-scope="scope">
              <router-link :to="{name: 'PeriodicTask', query: { interval_id: scope.row.id }}">
                <el-button type="text" size="small">查看任务</el-button>
              </router-link>
              <router-link :to="{name: EditName, query: { interval_id: scope.row.id }}">
                <el-button type="text" size="small">编辑</el-button>
              </router-link>
              <el-button type="text" size="small" @click="delObjs(scope.row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <div class="block paging">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          prev-text="上一页"
          next-text="下一页"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Interval',
    data() {
      return {
        // 需要搜索的项目名称
        inputObjName: '',  // 搜索功能实现的字符串
        // choices设置
        optionsPeriod: [],
        EditName: 'EditInterval',

        // 分页设置
        currentPage: 1, //当前页码
        pageSize: 10,    //    每页的条数
        pageSizes: [10, 20, 30, 40],  // 可选每页显示条数
        dataCount: 0,  // 数据总条数

        // 数据设置
        ListDataURL: this.$http.IntervalURL,  // 表格列表数据获取
        objID: 'interval_id',
        tableData: [],  // 阿贾克斯获取的初始数据
        multipleSelection: [],  // 不能删，我也不知道是干嘛用的，删了会报错
      }
    },
    created() {
      // 发送啊假克丝请求
      this.objList();// 阿贾克斯获取的初始数据
      this.optionsPeriod = this.getChoices.task.period;

    },

    methods: {
      handleClick(row) {
        console.log(row);
      },
      // 修改每页显示条数
      handleSizeChange(val) {
        this.pageSize = val;  // 修改每页显示条数
        this.currentPage = 1;  // 修改页码
      },
      // 修改页码
      handleCurrentChange(val) {
        this.currentPage = val;  // 修改页码
      },

      // 发送啊假克丝请求 得到表格的原始数据
      objList() {
        this.$myTool.objList(this)
      },
      // 删除函数，调用了删除接口
      delObjs(objId) {
        this.$myTool.delObjs(objId, this)
      },
      // 不能删
      handleSelectionChange(val) {
        this.multipleSelection = val;
        console.log(this.multipleSelection)
      },
      //  按项目名称搜索的函数
      inputFunc() {
        let queryData = {
          'page': this.currentPage,
          'size': this.pageSize,
          'inputObjName': this.inputObjName
        };
        this.$http.getApiFunc(this.$http.getApiStatus)(this.ListDataURL, queryData)
          .then(res => {
            if (!res.error_no) {
              this.tableData = res.data.results;
              this.dataCount = res.data.count;
            }
          })
          .catch(err => {
            console.log(err);
          })
      },
    },

    watch: {
      'pageSize': function (value) {
        this.objList();
      },
      'currentPage': function (value) {
        this.objList();
      }
    },

    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>

<style scoped>
  .paging {
    padding-top: 20px;
  }

</style>
